package io.renren.modules.car.service.impl;

import io.renren.modules.car.entity.CarBaseEntity;
import io.renren.modules.car.entity.CarShopMoneyEntity;
import io.renren.modules.car.service.CarShopMoneyService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.car.dao.CarSellMsgDao;
import io.renren.modules.car.entity.CarSellMsgEntity;
import io.renren.modules.car.service.CarSellMsgService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("carSellMsgService")
public class CarSellMsgServiceImpl extends ServiceImpl<CarSellMsgDao, CarSellMsgEntity> implements CarSellMsgService {

    @Resource
    private CarSellMsgDao carSellMsgDao;
    @Resource
    private CarShopMoneyService carShopMoneyService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CarSellMsgEntity> page = this.page(
                new Query<CarSellMsgEntity>().getPage(params),
                new QueryWrapper<CarSellMsgEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public boolean updateMoneyMsg(CarBaseEntity carBaseEntity) {
        CarSellMsgEntity carSellMsgEntity = new CarSellMsgEntity();
        carSellMsgEntity.setSellPrice(carBaseEntity.getSellPrice());
        carSellMsgEntity.setOtherPrice(carBaseEntity.getOtherPrice());
        carSellMsgEntity.setRemark(carBaseEntity.getSellRemark());
        carSellMsgEntity.setLastMoney(carBaseEntity.getLastMoney());
        carSellMsgEntity.setState(carBaseEntity.getState());
        carSellMsgEntity.setCarId(carBaseEntity.getId());

        boolean b = update(carSellMsgEntity, new QueryWrapper<CarSellMsgEntity>().eq("car_id", carBaseEntity.getId()));

        if (!Objects.isNull(carBaseEntity.getCarShopMoneyEntities())){
            List<CarShopMoneyEntity> carShopMoneyEntities = carBaseEntity.getCarShopMoneyEntities();
            carShopMoneyService.updateBatchById(carShopMoneyEntities);
        }

        return b;
    }
}
